package com.lgr.store.service;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lgr.store.dao.OrdersDAO;
import com.lgr.store.utils.PageObject;
import com.lgr.store.vo.DepVo;
import com.lgr.store.vo.MaterialVo;
import com.lgr.store.vo.OrderDetailVo;
import com.lgr.store.vo.OrdersVo;
import com.lgr.store.vo.StorageVo;
import com.lgr.store.vo.SupplierVo;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class OrderService {
    @Resource
    OrdersDAO orderDAO;

    //查询
    @Transactional
    public List<OrdersVo> list(OrdersVo order, PageObject pager){
        List<OrdersVo> orderList = orderDAO.listAll(order,pager);
        return orderList;
    }
    //查询记录数
    @Transactional
    public int count(OrdersVo order){
        return orderDAO.getcount(order);
    }
    //新增
    @Transactional
    public void add(OrdersVo order,List<OrderDetailVo> detailList){
        orderDAO.addOrder(order);
        for (OrderDetailVo detail : detailList) {
        	orderDAO.addDetail(detail);
    		updateDetailCnt(detail);
		}
    }

    //获取一条数据
    @Transactional
    public OrdersVo findById(String orderNo){
        return orderDAO.findById(orderNo);
    }
    //获取订单的明细数据
    @Transactional
    public List<OrderDetailVo> findByOrderNo(String orderNo){
        return orderDAO.findByOrderNo(orderNo);
    }
    //判断该批号的物料是否入库
    @Transactional
    public int findByBatchNo(String batchNo,int orderType,String orderFlag){
        return  orderDAO.findByBatchNo(batchNo,orderType,orderFlag);
    }
  //获取所有供应商
    @Transactional
    public List<SupplierVo> findAllSupplier(){
    	return orderDAO.findAllSupplier();
    }
    //获取所有仓库
    @Transactional
    public List<StorageVo> findAllStorage(){
    	return orderDAO.findAllStorage();
    }
    //模糊查询物料
    @Transactional
    public List<MaterialVo> findMaterialByCode(String materialCode){
    	return orderDAO.findMaterialByCode(materialCode);
    }
    //通过code查询物料
    @Transactional
    public MaterialVo findMaterialByCode2(int materialId){
    	return orderDAO.findMaterialByCode2(materialId);
    }

    
    //通过订单号或者批号从订单明细表查询
    @Transactional
    public List<OrderDetailVo>  findDetailByBatchNo(OrderDetailVo orderDetailVo){
    	return orderDAO.findDetailByBatchNo(orderDetailVo);
    }
    //通过订单号或者批号从订单明细表查询(退料使用)
    @Transactional
    public List<OrderDetailVo>  findDetailByBatchNo3(OrderDetailVo orderDetailVo){
    	return orderDAO.findDetailByBatchNo3(orderDetailVo);
    }
    @Transactional
    public String getOrderId(Map<String, String> params){
    	return orderDAO.getOrderId(params);
    }
    
    @Transactional
    public void del(String orderNo){
    	orderDAO.delOrderDetail(orderNo);
    	orderDAO.delOrder(orderNo);
    }
    //审核
    @Transactional
    public void updateAduit(OrdersVo order){
        orderDAO.updateAduit(order);
    }
    //修改状态
    @Transactional
    public void updateStatus(OrdersVo order){
        orderDAO.updateStat(order);
    }
    //按orderDetailId删除订单明细
    @Transactional
    public void delRow( Integer orderDetailId){
    	orderDAO.delRow(orderDetailId);
    }
    @Transactional
    public void updateOrder(OrdersVo order,List<OrderDetailVo> detailList){
    	orderDAO.updateOrder(order);
    	for (OrderDetailVo d : detailList) {
    		if(d.getOrderDetailId()>0){
    			orderDAO.updateDetail(d);
    		}else{
    			orderDAO.addDetail(d);
    		}
    		updateDetailCnt(d);
		}
    }
    //修改订单明细库存信息，仅仅在退料模块使用 
    @Transactional
    public void updateDetailCnt(OrderDetailVo detail){
    	OrderDetailVo dd = new OrderDetailVo();
		dd.setOrderCnt(detail.getOrderCnt());
    	orderDAO.updateDetailCnt(dd);
    }
    @Transactional
    public float getStorageCnt(OrderDetailVo detail){
    	
    	return orderDAO.getStorageCnt(detail);
    }
}